/**
 * Created by leo on 5/30/16.
 */

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var categorySchema = new Schema({
    name: {
        type: String,
        require: true
    },
    image_url: {
        type: String
    }
}, {
    collection: 'category',
    versionKey: false
});

var Category = module.exports = mongoose.model('Category', categorySchema);

// Get Categories
module.exports.getCategories = function(callback, limit) {
    Category.find(callback).limit(limit);
}

// Get Category By Id
module.exports.getCategoryById = function (id, callback) {
    Category.findById(id, callback);
}

// Add Category
module.exports.addCategory = function (category, callback) {
    Category.create(category, callback);
}

// Update Category
module.exports.updateCategory = function (id, category, options, callback) {
    var query = {_id: id};
    //var update = {
    //    name: category.name,
    //    image_url: category.image_url,
    //    devices: category.devices
    //}
    Category.findOneAndUpdate(query, category, options, callback);
}

// Delete Category
module.exports.removeCategory = function (id, callback) {
    var query = {_id: id};
    Category.remove(query, callback);
}
